package com.networklib.task.impl;

import android.content.Context;

import com.networklib.TaskResult;
import com.networklib.http.ResponseResult;
import com.networklib.http.exception.NetException;
import com.networklib.http.impl.BaseHttpRequestImpl;
import com.networklib.http.impl.ProgressRequestBodyImpl;
import com.networklib.http.impl.ProgressResponseBodyImpl;
import com.networklib.http.impl.get.SimpleGetHttpRequestImpl;
import com.networklib.http.impl.post.MutiPostHttpRequestImpl;
import com.networklib.http.impl.post.SinglePostHttpRequestImpl;
import com.networklib.http.other.HttpMethod;
import com.networklib.http.other.NetUtil;
import com.networklib.task.TaskResultCode;
import com.networklib.task.base.BaseTask;

import java.io.IOException;
import java.util.Map;

public class SimpleHttpTask extends BaseTask {
    private final Map<String, Object> params;
    private final String url;
    private final HttpMethod method;
    private final Context context;
    protected BaseHttpRequestImpl request;

    public SimpleHttpTask(Context context,String url, HttpMethod method, Object o) throws NetException {
        this.context = context;
        this.url = url;
        this.method = method;
        this.params = null;
        if (method == HttpMethod.POST) {
            request = new SinglePostHttpRequestImpl(url, o);
        } else {
            throw new NetException("访问方法必须是POST");
        }
    }

    public SimpleHttpTask(Context context,String url, HttpMethod method, Map params) throws NetException {
        this.context = context;
        this.url = url;
        this.method = method;
        this.params = params;
        if (method == HttpMethod.POST) {
            request = new MutiPostHttpRequestImpl(url, params);
        } else if (method == HttpMethod.GET) {
            request = new SimpleGetHttpRequestImpl(url, params);
        } else {
            throw new NetException("访问方法必须是GET或POST");
        }
    }

    @Override
    protected TaskResult doExecute(TaskResult taskResult) {
        try {
            ResponseResult execute = request.execute();
            taskResult.setResultCode(TaskResultCode.SUCCESS);
            taskResult.setResult(execute.getResponse());
//            if (execute.getResponse().body() != null) {
//                taskResult.setResult(execute.getResponse().body().string());
//            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return taskResult;
    }

    @Override
    protected boolean before(TaskResult taskResult) {
        boolean b = NetUtil.activeNetworkIsAvailable(context);
        if (!b) {
            TaskResultCode failure = TaskResultCode.FAILURE;
            failure.setOtherMsg("网络不可用");
            taskResult.setResultCode(failure);
            return false;
        }
        return true;
    }

    @Override
    protected void after(TaskResult taskResult) {

    }

    public BaseHttpRequestImpl getRequest() {
        return request;
    }


    public void addDownProgressListener(ProgressResponseBodyImpl.ProgressListener downProgressListener) {
        this.addProgressListener(null, downProgressListener);
    }

    public void addUpProgressListener(ProgressRequestBodyImpl.ProgressListener upProgressListener) {
        this.addProgressListener(upProgressListener, null);
    }

    public void addProgressListener(ProgressRequestBodyImpl.ProgressListener upProgressListener, ProgressResponseBodyImpl.ProgressListener downProgressListener) {
        this.request.setOnProgressListener(upProgressListener, downProgressListener);
    }
}
